﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

using eSYSU.Web.Business;
using eSYSU.Web.Common;
using eSYSU.Web.Entity;


/// <summary>
/// eSYSU文章内容相关的网络服务
/// </summary>
[WebService(Namespace = "http://esysu.com/ws/", Name = "eSYSU News Related Web Service",
	Description = "中大e家新闻相关服务的网络服务，使用本服务请先提交使用申请并在使用时遵循中大e家网络服务协议。")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[SoapDocumentService(RoutingStyle = SoapServiceRoutingStyle.RequestElement)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。 
// [System.Web.Script.Services.ScriptService]
public class ArticleWebService : System.Web.Services.WebService
{

	public ArticleWebService()
	{

		//如果使用设计的组件，请取消注释以下行 
		//InitializeComponent(); 
	}

	/// <summary>
	/// 获取新闻列表
	/// </summary>
	/// <param name="iIndex">新闻起始索引</param>
	/// <param name="iCount">新闻数量</param>
	/// <param name="iCategory">新闻类别ID</param>
	/// <returns>包含一个文章列表XML结构的字符串</returns>
	[WebMethod(CacheDuration = 3600, Description = "获取新闻列表", EnableSession = false)]
	public string GetNewsList(int iIndex, int iCount, int iCategory)
	{
		DataTable dt = ArticleService.GetLatestNewsBrief(iCount);
		dt.TableName = "NewsList";
		return StringHelper.ConvertDataTableToXML(dt);
	}

	/// <summary>
	/// 获取用户订阅的新闻列表
	/// </summary>
	/// <param name="sUserId">用户Id</param>
	/// <param name="iIndex">新闻起始索引</param>
	/// <param name="iCount">新闻数量</param>
	/// <returns>包含一个文章列表XML结构的字符串</returns>
	[WebMethod(CacheDuration = 3600, Description = "获取用户订阅的新闻列表", EnableSession = false)]
	public string GetSubsNewsList(string sUserId, int iIndex, int iCount)
	{
		DataTable dt = ArticleService.GetUserSubsciptionNews(sUserId, iIndex, iCount);
		dt.TableName = "SubsNewsList";
		return StringHelper.ConvertDataTableToXML(dt);
	}


	/// <summary>
	/// 获取单条新闻的内容
	/// </summary>
	/// <param name="iNewsID">新闻ID</param>
	/// <param name="iImageSize">图片大小（0~4）</param>
	/// <returns>包含指定新闻内容XML结构的字符串</returns>
	[WebMethod(Description = "获取单条新闻的内容")]
	public string GetNewsContent(int iNewsID, int iImageSize)
	{
		return ArticleService.GetNewsById(iNewsID).ToXmlString(iImageSize);
	}


	/// <summary>
	/// 获取学院列表
	/// </summary>
	/// <returns>包含学院列表的XML结构的字符串</returns>
	[WebMethod(Description = "获取学院列表")]
	public string GetCollegeList()
	{
		return StringHelper.ConvertDataTableToXML(UserService.GetCollegeList());
	}


	/// <summary>
	/// 根据新闻ID或用户名获取新闻评论列表
	/// </summary>
	/// <param name="iNewsID">新闻ID</param>
	/// <param name="sUName">用户名</param>
	/// <returns>包含评论列表XML结构的字符串</returns>
	[WebMethod(Description = "根据新闻ID或用户名获取新闻评论列表")]
	public string GetNewsCommentList(int iNewsID, string sUName)
	{
		if (iNewsID > 0)
		{
			DataTable newsList = CommentsService.GetCommentsByNewsID(iNewsID);
			return StringHelper.ConvertDataTableToXML(newsList);
		}
		else
		{
			return StringHelper.ConvertDataTableToXML(CommentsService.GetCommentByUsername(sUName));
		}
	}


	/// <summary>
	/// 插入一条新评论(暂不启用)
	/// </summary>
	/// <param name="xmlComment">包含评论内容的XML结构字符串</param>
	/// <returns>插入结果</returns>
	[WebMethod(Description = "插入一条新评论")]
	public bool InsertNewsComment(string xmlComment)
	{
		Comments comment = new Comments();
		comment = CommentsService.LoadXml(xmlComment);
		//NewsCommentService.InserNewsComment(comment);
		return true;
	}
}
